package com.swf.seed.netty.test;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.http.HttpRequest;

public class HttpClient {
 
    private ClientBootstrap bootstrap;
    private String host="localhost";
    private Channel channel;
    private boolean futureSuccess;
    private int port=8080;
 
    public HttpClient() {
    }
 
    public ChannelFuture connect() {
        bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors
                .newCachedThreadPool()));
        HttpResponseHandler clientHandler = new HttpResponseHandler();
        bootstrap.setPipelineFactory(new HttpClientPipelineFactory(clientHandler));
 
        bootstrap.setOption("tcpNoDelay", true);
        bootstrap.setOption("keepAlive", true);
 
        return bootstrap.connect(new InetSocketAddress(host,port));
    }
     
    public boolean checkFutureState(ChannelFuture channelFuture) {
        // Wait until the connection attempt succeeds or fails.
        channel = channelFuture.awaitUninterruptibly().getChannel();
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture connectFuture) throws Exception {
                if (!connectFuture.isSuccess()) {
                    connectFuture.getCause().printStackTrace();
                    // connectFuture.getChannel().close();
                    // bootstrap.releaseExternalResources();
                    futureSuccess = false;
                } else {
                    futureSuccess = true;
                }
            }
        });
        return futureSuccess;
    }
     
    public ChannelFuture write(HttpRequest request) {
        return channel.write(request);
    }
     
    public void Close() {
        // Close the connection. Make sure the close operation ends because
        // all I/O operations are asynchronous in Netty.
        channel.close().awaitUninterruptibly();
        // Shut down all thread pools to exit.
        bootstrap.releaseExternalResources();
    }
}